
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  sort;


import logger.PoolLogger;
import dbmanager.DBManager;
import validation.Isnumeric;


public class BufferSorting 
{
					 
	String split[],strValue[];
	private int index;
	int intValue[];
	PoolLogger pl;
	
	public BufferSorting() {
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	public String[] bufferSorting(String buffer[],String str)
	{
	
		String[] splitStr=buffer[0].split("\t");
		split=new String[splitStr.length];
		strValue=new String[buffer.length-1];
		//Isnumeric isn=new Isnumeric();
		intValue = new int[buffer.length-1];
		split=buffer[0].split("\t");
		for(int i=0;i<split.length;i++)
		{			
			if(split[i].toLowerCase().equals(str.toLowerCase()))
			{
				index=i;
				//System.out.println("index1 is "+index1);
			}
		}
		
		for(int i=1;i<buffer.length;i++)
		{
			split=buffer[i].split("\t");		
			strValue[i-1]=split[index];
			//System.out.println(strValue[i-1]);
		}
		//for(int i =0;i<split.length;i++)
		//	System.out.println(strValue[i]);
		for(int i=0;i<buffer.length-1;i++)
		{
			//if(isn.validate(strValue[i])==true)
			{
			//intValue[i]=Integer.parseInt(strValue[i]);
		 	//System.out.print(intValue[i]+" ");
			}
			//else
			{
				strValue[i]=strValue[i];
				//System.out.println(strValue[i]);
			}
			
		}
	

		

		if(intValue[0]!=0)
		{
			sortIds(intValue);
			String intStr[]=new String[intValue.length];
			for (int j = 0; j < buffer.length-1; j++)
			{
				Integer i=new Integer(intValue[j]);
					intStr[j]=i.toString();
				//System.out.print(intValue[j] + " ");
			}
			return intStr;			
		}
		
		else
		{
			sortStrings(strValue,strValue.length);
			return strValue;
			//for(int x = 0; x < buffer.length-1; ++x)
			//	System.out.print(strValue[x]+" ");	
		}	 
	
	
}


	void sortIds(int a[])
	{
		for (int i = a.length; --i>=0; )
		{		           
		    for (int j = 0; j<i; j++)
		    {
		    	if (a[j] > a[j+1]) 
		    	{
		    		int T = a[j];
		    		a[j] = a[j+1];
		    		a[j+1] = T;
			   }
		    }			
		}   
		//for(int x = 0; x < a.length; ++x)
					//System.out.print(a[x]+" ");
		
	}

	 void sortStrings(String[] array, int len)
	{		
	int a, b;
	String temp;
	int highSubscript = len - 1;
	for(a = 0; a < highSubscript; ++a)
		for(b = 0; b < highSubscript; ++b)
			if(array[b].toLowerCase().compareTo(array[b + 1].toLowerCase()) > 0)
			{
				temp = array[b];
				array[b] = array[b + 1];
				array[b + 1] = temp;
			}	
	}		
		
		
	
	
	public static void main(String args[])
	{			
		String buffer[]=new String[]{"id\tName\tAddr\tDesig\tPhone\tCas",
									 "1\tshweta\tpune\tsw\t111201\tH",
									 "2\tmegha\tbanglore\thr\t101301\tH",
									 "3\tReena\tDelhi\thw\t101\tH",
									 "4\theena\tpatna\tst\t1123\tG",
									 "5\tvrinda\tkolkata\tst\t10\tG",
									 "6\tkomal\traipur\thw\t11223\tH"
				 					};
			String str="phone";
		
			BufferSorting bfs=new BufferSorting();
			buffer=bfs.bufferSorting(buffer,str);
			for(int x = 0; x < buffer.length; ++x)
					System.out.print(buffer[x]+" ");	
			
	}
}
